/*
 * Created on Mar 14, 2004
 */
package com.scalar.chiptrack.operations.packingslip.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.packingslip.dao.NewPackingSlipDAO;
import com.scalar.chiptrack.operations.packingslip.forms.PackingSlipForm;
import com.scalar.chiptrack.users.User;

/**
 * @author Ravikanth
**/
public class CancelPackingSlipAction extends ChipTrackAction
{
	public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception 
	{
		HttpSession session = request.getSession();
		String returnVal 	= "failure";
		String action 		= request.getParameter( "action" );
		String currentUser 	= null; 
		boolean dropShip	= false;
		boolean success 	= false;
		int customerShippingId  	= -1;
		HashMap errors				= new HashMap();
		HashMap info 				= new HashMap();
		HashMap lineItemMap 		= null;
		HashMap finalInventoryMap 	= null;
		PackingSlipForm packingSlipForm = (PackingSlipForm) form;
 		
		User user 		   	= (User) session.getAttribute ( SessionKeys.USER_KEY );
		currentUser 		= user.getUserId();
		customerShippingId 	= packingSlipForm.getCustomerShippingId();

		DBTransaction dbTransaction = null;
		
		try
		{
			dbTransaction = DBTransaction.getInstance();
			dbTransaction.begin();
			
			// Update the Customer Shipping Tables	
			NewPackingSlipDAO.cancelPackingSlip ( dbTransaction, customerShippingId, user.getUserId() );
			// Update the Customer Shipping Table set Show Commercial Invoice to 1 
			// CommercialInvoiceDAO.deleteCommercialInovice( dbTransaction, customerShippingId );
			info.put ( "PackingSlip (" + packingSlipForm.getLabel() + ") has been cancelled", null );
			request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
			success = true;
		}
		catch (ScalarException se) 
		{
			// Could not delete the packing slip
			se.printStackTrace();
			errors.put ( "Internal Server Error", "Could not cancel packingslip (" + packingSlipForm.getLabel() + ")" );
			errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
			session.setAttribute( SessionKeys.ERROR_REPORT_SUBJECT, "Error: Could not cancel packingslip (" + packingSlipForm.getLabel() + ")" );
			session.setAttribute( SessionKeys.ERROR_REPORT, se );
			request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
		}
		finally
		{
			// Rollback or Commit the Transaction
			if (success == true)
			{
				dbTransaction.commit();
			}
			else
			{
				dbTransaction.rollback();
			}
		}
		return ( mapping.findForward ( "packingSlipMenu" ) );
	}
}